package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description DishController
 * @Author zhuzi
 * @Date 2023/09/25
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result<?> createDish(@RequestBody DishDTO dishDTO) {
        log.info("Get DishDTO: {}", dishDTO);
        dishService.createDish(dishDTO);

        // 清理缓存
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("Get DishPageQueryDTO: {}", dishPageQueryDTO);
        return Result.success(dishService.page(dishPageQueryDTO));
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("Get CategoryId: {}", categoryId);
        return Result.success(dishService.list(categoryId));
    }

    /**
     * 启用或禁用菜品
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<?> pickOrBan(@PathVariable Integer status, Long id) {
        log.info("Get status: {}, id: {}", status, id);
        dishService.pickOrBan(status, id);

        // 清理缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id获取菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("Get id: {}", id);
        return Result.success(dishService.getDishById(id));
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result<?> updateDish(@RequestBody DishDTO dishDTO) {
        log.info("Get DishDTO: {}", dishDTO);
        dishService.updateDish(dishDTO);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<?> deleteDish(@RequestParam List<Long> ids) {
        log.info("Get ids: {}", ids);
        dishService.deleteDish(ids);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     *
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
